package com.example.utils;


import org.meowy.cqp.jcq.entity.CQImage;

import java.util.*;

/**
 * @author LEI
 * Created by LEI on 2020/3/16.
 */
public class ListUtils {

    /**获取int列表中最频繁的int
     * @param integerList
     * @return
     */
    public static int getListMaxFrequentlyInteger( List<Integer> integerList){
        Map<Integer,Integer> map = new HashMap<Integer, Integer>(integerList.size());
        for (Integer integer : integerList) {
            if(map.containsKey(integer)){
                map.put(integer,map.get(integer)+1);
            }else{
                map.put(integer,1);
            }
        }
        Iterator<Integer> iterator = map.keySet().iterator();
        int keyMax = -1;
        int temp = -1;
        while (iterator.hasNext()){
            Integer key = iterator.next();
            Integer value = map.get(key);
            if(value>keyMax){
                temp = key;
                keyMax = value;
            }
        }
        return temp;
    }

    /**获取long列表中最频繁的long
     * @param longList
     * @return
     */
    public static Long getListMaxFrequentlyLong( List<Long> longList){
        Map<Long,Integer> map = new HashMap<Long, Integer>(longList.size());
        for (Long lo : longList) {
            if(map.containsKey(lo)){
                map.put(lo,map.get(lo)+1);
            }else{
                map.put(lo,1);
            }
        }
        Iterator<Long> iterator = map.keySet().iterator();
        int keyMax = -1;
        Long temp = -1L;
        while (iterator.hasNext()){
            Long key = iterator.next();
            Integer value = map.get(key);
            if(value>keyMax){
                temp = key;
                keyMax = value;
            }
        }
        return temp;
    }

    /**获取字符串中最频繁的一串字符串
     * @param stringList  字符串列表
     * @return 最频繁的字符串
     */
    public static String getListMaxFrequentlyString( List<String> stringList){
        Map<String,Integer> map = new HashMap<String, Integer>(stringList.size());
        for (String string : stringList) {
            if(map.containsKey(string)){
                map.put(string,map.get(string)+1);
            }else{
                map.put(string,1);
            }
        }
        Iterator<String> iterator = map.keySet().iterator();
        int keyMax = -1;
        String temp = "";
        while (iterator.hasNext()){
            String key = iterator.next();
            Integer value = map.get(key);
            if(value>keyMax){
                temp = key;
                keyMax = value;
            }
        }
        return temp;
    }

    /**获取list中最频繁的一张图片
     * @param cqImages  图片list
     * @return 最频繁的一张图片
     */
    public static CQImage getListMaxFrequentlyCQImage(List<CQImage> cqImages){
        Map<String,Integer> map = new HashMap<String, Integer>(cqImages.size());
        Map<String,CQImage> cqImageMap = new HashMap<String, CQImage>();
        for (CQImage cqImage : cqImages) {
            String key = cqImage.getName();
            if(map.containsKey(key)){
                map.put(key,map.get(key)+1);
            }else{
                map.put(cqImage.getName(),1);
            }
            cqImageMap.put(key,cqImage);
        }
        Iterator<String> iterator = map.keySet().iterator();
        int keyMax = -1;
        String temp = "";
        while (iterator.hasNext()){
            String key = iterator.next();
            Integer value = map.get(key);
            if(value>keyMax){
                temp = key;
                keyMax = value;
            }
        }
        return cqImageMap.get(temp);
    }

}
